package org.prs.persistence;

import com.prs.util.PrsException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.prs.model.RelationshipType;
import org.prs.persistence.connector.MySQLDatabaseConnector;

public class RelationshipTypeDatabasePersitence {

    private Connection conn;
    private ResultSet result;

    public RelationshipTypeDatabasePersitence() {
    }

    public boolean insertRelationshipType(RelationshipType relationshipType) {
        boolean inserted = true;
        conn = MySQLDatabaseConnector.getConnection();
        PreparedStatement insertRelationship = null;
        try {
            insertRelationship = conn.prepareStatement("insert into relationshiptype set name=?, description=? ");
            insertRelationship.setString(1, relationshipType.getName());
            insertRelationship.setString(2, relationshipType.getDescription());
            insertRelationship.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            inserted = false;
        }
        return inserted;
    }

    public List<RelationshipType> getAllRelationshipTypes() {
        List<RelationshipType> relationshipTypes = new ArrayList<RelationshipType>();
        conn = MySQLDatabaseConnector.getConnection();
        PreparedStatement selectRelationshipTypes = null;
        try {
            selectRelationshipTypes = conn.prepareStatement("select * from relationshiptype ");
            result = selectRelationshipTypes.executeQuery();
            while (result.next()) {
                relationshipTypes.add(getRelationshipType(result));
            }
        } catch (SQLException e) {
            e.printStackTrace();

        }
        return relationshipTypes;
    }

    public RelationshipType getRelationshipType(int relationshipTypeId) {
        RelationshipType relationshipType = new RelationshipType();
        conn = MySQLDatabaseConnector.getConnection();
        PreparedStatement selectRelationshipTypeById = null;
        try {
            selectRelationshipTypeById = conn.prepareStatement("select * from relationshiptype where relationshiptypeid=?");
            selectRelationshipTypeById.setInt(1, relationshipTypeId);
            result = selectRelationshipTypeById.executeQuery();
            while (result.next()) {
                relationshipType = getRelationshipType(result);
            }
        } catch (SQLException e) {
            throw new PrsException(e);

        }
        return relationshipType;
    }

    private RelationshipType getRelationshipType(ResultSet result) {
        RelationshipType relationshipType = new RelationshipType();
        try {
            relationshipType = new RelationshipType();
            relationshipType.setName(result.getString("name"));
            relationshipType.setDescription(result.getString("description"));
            relationshipType.setRelationshipTypeId(result.getInt("relationshiptypeid"));
        } catch (SQLException e) {
            throw new PrsException(e);

        }
        return relationshipType;
    }
}
